package com.lenovo.mso2o;

/**
 * Created by hippo on 1/28/16.
 */

import android.view.View;
import android.widget.Button;

import com.lenovo.mso2o.ui.MainActivity;

import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowToast;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml")
public class MainActivityTest  extends TestCase{

    private MainActivity activity;

    @Test
    public void shouldHaveHappySmiles() throws Exception {
        //String hello = new MainActivity().getResources().getString(R.string.hello_world);
        String hello = "Hello world!";
        assertThat(hello, equalTo("Hello world!"));
    }

    @Before
    public void setup() {
        activity = Robolectric.buildActivity(MainActivity.class)
                .create().get();
    }

    @Test
    public void checkActivityNotNull() throws Exception {
        assertNotNull(activity);
    }

    @Test
    public void buttonClickShouldStartNewActivity() throws Exception {
        View button =  activity.findViewById(R.id.actionbar_left);
        button.performClick();
    }

    @Test
    public void testButtonClick() throws Exception {
        MainActivity activity = Robolectric.buildActivity(MainActivity.class)
                .create().get();
        Button view = (Button) activity.findViewById(R.id.button1);
        assertNotNull(view);
        view.performClick();
        assertThat(ShadowToast.getTextOfLatestToast(), equalTo("Lala"));
    }

}
